
import { _decorator, Component, Node, Collider, ITriggerEvent } from 'cc';
import { Constant } from '../framework/Constant';
import { GameManager } from '../framework/GameManager';
import { PoolManager } from '../framework/PoolManager';
const { ccclass, property, menu } = _decorator;

/**
 * Predefined variables
 * Name = EnemyPlane
 * DateTime = Mon Feb 14 2022 19:51:47 GMT+0800 (中国标准时间)
 * Author = ashscc
 * FileBasename = EnemyPlane.ts
 * FileBasenameNoExtension = EnemyPlane
 * URL = db://assets/script/plane/EnemyPlane.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/en/
 *
 */
const OUTOFBOUNCE_Z = 45 //敌机Z坐标超出屏幕的坐标值

@ccclass('EnemyPlane')
@menu('plane/EnemyPlane')
export class EnemyPlane extends Component {
    private _speed = 0
    private _isShooting: boolean = false //是否发射子弹(组合1发射子弹，组合2、3不发射子弹)
    private _gameManager: GameManager = null

    private _bulletCreatePeriod = 0.3
    private _currentBulletCreateTime = 0

    onEnable(){
        const collider = this.node.getComponent(Collider)
        collider.on('onTriggerEnter', this._onTriggerEnter, this)
    }

    onDisable(){
        const collider = this.node.getComponent(Collider)
        collider.off('onTriggerEnter', this._onTriggerEnter, this)
    }

    start(){
        this._currentBulletCreateTime = this._bulletCreatePeriod //开始就发射一颗子弹
    }

    update (deltaTime: number) {
        const pos = this.node.position
        const pos_z = pos.z + this._speed
        this.node.setPosition(pos.x, pos.y, pos_z)

        if(this._isShooting){
            this._currentBulletCreateTime += deltaTime
            if(this._currentBulletCreateTime > this._bulletCreatePeriod){
                this._currentBulletCreateTime = 0
                this._gameManager.createEnemyBullet(this.node.position, this.node.name)
            }
        }

        if (pos_z > OUTOFBOUNCE_Z) {
            PoolManager.putNode(this.node)
            // this.node.destroy()
        }
    }


    private _onTriggerEnter(event: ITriggerEvent) {
        const otherColliderGroup = event.otherCollider.getGroup()
        if (otherColliderGroup === Constant.CollisionType.SELF_PLAYER || otherColliderGroup === Constant.CollisionType.SELF_BULLET){
            this._gameManager.play('enemy')
            this._gameManager.addScore()
            PoolManager.putNode(this.node)
            this._gameManager.createEnemyExplodeEffect(this.node.position)
            // this.node.destroy()
        }
    }

    show(gameManager: GameManager, speed: number, isShooting: boolean){
        this._gameManager = gameManager
        this._speed = speed
        this._isShooting = isShooting
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/en/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/en/scripting/life-cycle-callbacks.html
 */
